import api from '@/api'

import { getToken } from '@/utils/auth'

const state = {
  token: getToken(),

  isEmployee: false,
  partner: {},
  uid: undefined,
  partner_id: undefined
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },

  SET_SessionInfo: (state, { uid, partner_id }) => {
    state.uid = uid
    state.partner_id = partner_id
  },

  SET_ME: (state, { partner, uid, partner_id }) => {
    state.partner = { ...partner }
    state.isEmployee = partner.employee
    state.uid = uid
    state.partner_id = partner_id
  }
}

const actions = {
  async getSessionInfo({ commit, state, dispatch }) {
    const session = await api.rpc.get_session_info(state.token)
    commit('SET_SessionInfo', session)
    await dispatch('getInfo')
    return new Promise((resolve, reject) => {
      resolve(session)
    })
  },

  async getInfo({ commit, state, dispatch }) {
    const { uid, partner_id } = await api.get_userinfo()
    // console.log('me getInfo', uid, partner_id)
    const Ptn = api.env('res.partner')
    // const fs = await Ptn.fields_get()
    // console.log('ptn, fs,', fs)

    const partner = await Ptn.browse_one(partner_id, {
      fields: {
        name: null,
        employee: null,
        title: null
      }
    })
    console.log('xxxx, getInfo', partner)
    commit('SET_ME', { partner, uid, partner_id })
    return new Promise(resolve => {
      resolve({ uid, partner_id, partner })
    })
  },

  // user login
  async login({ commit, dispatch }, userInfo) {
    const { username, password } = userInfo
    const response = await api.rpc.login({
      username: username.trim(),
      password: password
    })
    // console.log('xxxxx, login,', dispatch)
    return dispatch('afterLogin', response)
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      api.rpc
        .logout(state.token)
        .then(() => {
          dispatch('resetToken')
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  async afterLogin({ commit }, response) {
    return new Promise((resolve, reject) => {
      const { token } = response
      if (token) {
        commit('SET_TOKEN', token)
        resolve()
      } else {
        console.log('xxxx, afterLogin,reject,', response)
        reject(response)
      }
    })
  },

  // remove token
  async resetToken({ commit }) {
    console.log('resetToken')
    await api.rpc.reset_token()
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      resolve()
    })
  },

  async smsSend({ commit }, payload) {
    // const { username, password } = userInfo
    const res = await api.rpc.sms_send(payload)
    return new Promise((resolve, reject) => {
      resolve(res)
    })
  },

  async smsBack({ commit }, payload) {
    const code = await api.rpc.sms_back(payload)
    return new Promise((resolve, reject) => {
      resolve(code)
    })
  },

  async smsLogin({ commit, dispatch }, payload) {
    const response = await api.rpc.sms_login(payload)
    return dispatch('afterLogin', response)
  },

  async register({ commit, dispatch }, payload) {
    // const { mobile, code, username, password } = payload
    const response = api.rpc.register(payload)
    return new Promise((resolve, reject) => {
      response
        .then(res => {
          dispatch('resetToken')
          resolve(res)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  async resetPassword({ commit, dispatch }, payload) {
    // const { mobile, code, password } = payload
    // console.log('xxxx, resetPassword, ', mobile, code, password)

    const response = api.rpc.sms_reset_password(payload)

    return new Promise((resolve, reject) => {
      response
        .then(res => {
          dispatch('resetToken')
          resolve(res)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  async changePassword({ commit, dispatch }, payload) {
    const { oldPsw, newPsw } = payload
    const User = api.env('res.users')

    const response = User.change_password(oldPsw, newPsw)

    return new Promise((resolve, reject) => {
      response
        .then(res => {
          dispatch('resetToken')
          resolve(res)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
